package gui;

import javax.swing.JFrame;
import javax.swing.JPanel;

import java.awt.GridBagLayout;
import javax.swing.JButton;

import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JTextArea;


public class Main extends JPanel implements KeyListener {
	private JTextArea txtNum;
	
	private JButton btnNumber9;
	private JButton btnNumber8;
	private JButton btnNumber7;
	private JButton btnNumber6;
	private JButton btnNumber5;
	private JButton btnNumber4;
	private JButton btnNumber3;
	private JButton btnNumber2;
	private JButton btnNumber1;
	private JButton btnNumber0;
	
	private JButton btnPlus;
	private JButton btnMinus;
	private JButton btnDivide;
	private JButton btnMultiply;
	private JButton btnEnter;
	private JButton btnClearScrn;
	private JButton btnDecimalpoint;
	
	private int op=-1;
	private double num1, num2;
	private String str1="", str2="";
	private boolean boo1 = false, boo2 = false; //Set to true when a . is pressed
	private boolean numDone = false;
	private boolean opOnScreen = false;
	private static Main main;
	
	
	public static void main(String args[]){
		main = new Main();

		JFrame frame = new JFrame("Ash's Proulator");
		frame.getContentPane().add(main);
		frame.setVisible(true);
		frame.setSize(300, 300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	public Main(){		
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{30, 30, 30, 30}; //this line sets width of y-axis columns
		gridBagLayout.rowHeights = new int[]{40, 20, 20, 20, 20, 20}; //height of the x-axis columns 
		gridBagLayout.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0};
		gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
		setLayout(gridBagLayout);	
		
		txtNum = new JTextArea();
		txtNum.setFont(new Font("Tahoma", Font.PLAIN, 20));
		txtNum.setEditable(false);
		txtNum.addKeyListener(this);
		txtNum.requestFocusInWindow();
		GridBagConstraints gbc_txtNum = new GridBagConstraints();
		gbc_txtNum.insets = new Insets(5, 5, 5, 0);
		gbc_txtNum.fill = GridBagConstraints.BOTH;
		gbc_txtNum.gridx = 0;
		gbc_txtNum.gridy = 0;
		gbc_txtNum.gridwidth = 4;
		add(txtNum, gbc_txtNum);
			
		
		//======================== KEY PAD ========================
		btnNumber9 = new JButton("9");
		btnNumber9.addActionListener(new Numbpad());
		
		GridBagConstraints gbc_btnNumber9 = new GridBagConstraints();
		gbc_btnNumber9.insets = new Insets(0, 0, 5, 5);
		gbc_btnNumber9.fill = GridBagConstraints.BOTH;
		gbc_btnNumber9.gridx = 2;
		gbc_btnNumber9.gridy = 2;
		add(btnNumber9, gbc_btnNumber9);
		
		btnNumber8 = new JButton("8");
		btnNumber8.addActionListener(new Numbpad());
		GridBagConstraints gbc_btnNumber8 = new GridBagConstraints();
		gbc_btnNumber8.insets = new Insets(0, 0, 5, 5);
		gbc_btnNumber8.fill = GridBagConstraints.BOTH;
		gbc_btnNumber8.gridx = 1;
		gbc_btnNumber8.gridy = 2;
		add(btnNumber8, gbc_btnNumber8);
		
		btnNumber7 = new JButton("7");
		btnNumber7.addActionListener(new Numbpad());
		GridBagConstraints gbc_btnNumber7 = new GridBagConstraints();
		gbc_btnNumber7.insets = new Insets(0, 0, 5, 5);
		gbc_btnNumber7.fill = GridBagConstraints.BOTH;
		gbc_btnNumber7.gridx = 0;
		gbc_btnNumber7.gridy = 2;
		add(btnNumber7, gbc_btnNumber7);
		
		btnNumber6 = new JButton("6");
		btnNumber6.addActionListener(new Numbpad());
		GridBagConstraints gbc_btnNumber6 = new GridBagConstraints();
		gbc_btnNumber6.insets = new Insets(0, 0, 5, 5);
		gbc_btnNumber6.fill = GridBagConstraints.BOTH;
		gbc_btnNumber6.gridx = 2;
		gbc_btnNumber6.gridy = 3;
		add(btnNumber6, gbc_btnNumber6);
		
		btnNumber5 = new JButton("5");
		btnNumber5.addActionListener(new Numbpad());
		GridBagConstraints gbc_btnNumber5 = new GridBagConstraints();
		gbc_btnNumber5.insets = new Insets(0, 0, 5, 5);
		gbc_btnNumber5.fill = GridBagConstraints.BOTH;
		gbc_btnNumber5.gridx = 1;
		gbc_btnNumber5.gridy = 3;
		add(btnNumber5, gbc_btnNumber5);
		
		btnNumber4 = new JButton("4");
		btnNumber4.addActionListener(new Numbpad());
		GridBagConstraints gbc_btnNumber4 = new GridBagConstraints();
		gbc_btnNumber4.insets = new Insets(0, 0, 5, 5);
		gbc_btnNumber4.fill = GridBagConstraints.BOTH;
		gbc_btnNumber4.gridx = 0;
		gbc_btnNumber4.gridy = 3;
		add(btnNumber4, gbc_btnNumber4);
		
		btnNumber3 = new JButton("3");
		btnNumber3.addActionListener(new Numbpad());
		GridBagConstraints gbc_btnNumber3 = new GridBagConstraints();
		gbc_btnNumber3.insets = new Insets(0, 0, 5, 5);
		gbc_btnNumber3.fill = GridBagConstraints.BOTH;
		gbc_btnNumber3.gridx = 2;
		gbc_btnNumber3.gridy = 4;
		add(btnNumber3, gbc_btnNumber3);
		
		btnNumber2 = new JButton("2");
		btnNumber2.addActionListener(new Numbpad());
		GridBagConstraints gbc_btnNumber2 = new GridBagConstraints();
		gbc_btnNumber2.insets = new Insets(0, 0, 5, 5);
		gbc_btnNumber2.fill = GridBagConstraints.BOTH;
		gbc_btnNumber2.gridx = 1;
		gbc_btnNumber2.gridy = 4;
		add(btnNumber2, gbc_btnNumber2);
		
		btnNumber1 = new JButton("1");
		btnNumber1.addActionListener(new Numbpad());
		GridBagConstraints gbc_btnNumber1 = new GridBagConstraints();
		gbc_btnNumber1.insets = new Insets(0, 0, 5, 5);
		gbc_btnNumber1.fill = GridBagConstraints.BOTH;
		gbc_btnNumber1.gridx = 0;
		gbc_btnNumber1.gridy = 4;
		add(btnNumber1, gbc_btnNumber1);
		
		btnNumber0 = new JButton("0");
		btnNumber0.addActionListener(new Numbpad());
		GridBagConstraints gbc_btnNumber0 = new GridBagConstraints();
		gbc_btnNumber0.insets = new Insets(0, 0, 0, 5);
		gbc_btnNumber0.fill = GridBagConstraints.BOTH;
		gbc_btnNumber0.gridx = 0;
		gbc_btnNumber0.gridy = 5;
		gbc_btnNumber0.gridwidth = 2;
		add(btnNumber0, gbc_btnNumber0);
		
		
		//=============== Other buttons =======================
		btnEnter = new JButton("=");
		btnEnter.addActionListener(new Enter());
		GridBagConstraints gbc_btnEnter = new GridBagConstraints();
		gbc_btnEnter.fill = GridBagConstraints.BOTH;
		gbc_btnEnter.gridx = 3;
		gbc_btnEnter.gridy = 4;
		gbc_btnEnter.gridheight = 2;
		add(btnEnter, gbc_btnEnter);
		
		btnClearScrn = new JButton("C");
		btnClearScrn.addActionListener(new Clear());
		GridBagConstraints gbc_btnClearScrn = new GridBagConstraints();
		gbc_btnClearScrn.insets = new Insets(0, 0, 5, 5);
		gbc_btnClearScrn.gridx = 0;
		gbc_btnClearScrn.gridy = 1;
		add(btnClearScrn, gbc_btnClearScrn);
		
		btnPlus = new JButton("+");
		btnPlus.addActionListener(new Operator());
		GridBagConstraints gbc_btnPlus = new GridBagConstraints();
		gbc_btnPlus.insets = new Insets(0, 0, 5, 0);
		gbc_btnPlus.fill = GridBagConstraints.BOTH;
		gbc_btnPlus.gridx = 3;
		gbc_btnPlus.gridy = 2;
		gbc_btnPlus.gridheight = 2;
		add(btnPlus, gbc_btnPlus);
		
		btnDivide = new JButton("/");
		btnDivide.addActionListener(new Operator());
		GridBagConstraints gbc_btnDivide = new GridBagConstraints();
		gbc_btnDivide.insets = new Insets(0, 0, 5, 5);
		gbc_btnDivide.fill = GridBagConstraints.BOTH;
		gbc_btnDivide.gridx = 1;
		gbc_btnDivide.gridy = 1;
		add(btnDivide, gbc_btnDivide);
		
		btnMultiply = new JButton("*");
		btnMultiply.addActionListener(new Operator());
		GridBagConstraints gbc_btnMultiply = new GridBagConstraints();
		gbc_btnMultiply.insets = new Insets(0, 0, 5, 5);
		gbc_btnMultiply.fill = GridBagConstraints.BOTH;
		gbc_btnMultiply.gridx = 2;
		gbc_btnMultiply.gridy = 1;
		add(btnMultiply, gbc_btnMultiply);
		
		btnMinus = new JButton("-");
		btnMinus.addActionListener(new Operator());
		GridBagConstraints gbc_btnMinus = new GridBagConstraints();
		gbc_btnMinus.insets = new Insets(0, 0, 5, 0);
		gbc_btnMinus.fill = GridBagConstraints.BOTH;
		gbc_btnMinus.gridx = 3;
		gbc_btnMinus.gridy = 1;
		add(btnMinus, gbc_btnMinus);
		
		btnDecimalpoint = new JButton(".");
		btnDecimalpoint.addActionListener(new Point());
		GridBagConstraints gbc_btnDecimalpoint = new GridBagConstraints();
		gbc_btnDecimalpoint.insets = new Insets(0, 0, 0, 5);
		gbc_btnDecimalpoint.fill = GridBagConstraints.BOTH;
		gbc_btnDecimalpoint.gridx = 2;
		gbc_btnDecimalpoint.gridy = 5;
		add(btnDecimalpoint, gbc_btnDecimalpoint);
	}

	
	public void keyPressed(KeyEvent arg0) {}
	public void keyTyped(KeyEvent arg0) {}
	public void keyReleased(KeyEvent arg0) {
		//System.out.println("" + arg0.getKeyCode() + " " + arg0.getKeyChar());
		if(arg0.getKeyCode() == 127) btnClearScrn.doClick();
		if(arg0.getKeyCode() == 111) btnDivide.doClick();
		if(arg0.getKeyCode() == 106) btnMultiply.doClick();
		if(arg0.getKeyCode() == 109) btnMinus.doClick();
		if(arg0.getKeyCode() == 107) btnPlus.doClick();
		if(arg0.getKeyCode() == 110) btnDecimalpoint.doClick();
		if(arg0.getKeyCode() == 10) btnEnter.doClick();
		if(arg0.getKeyCode() == 96) btnNumber0.doClick();
		if(arg0.getKeyCode() == 97) btnNumber1.doClick();
		if(arg0.getKeyCode() == 98) btnNumber2.doClick();
		if(arg0.getKeyCode() == 99) btnNumber3.doClick();
		if(arg0.getKeyCode() == 100) btnNumber4.doClick();
		if(arg0.getKeyCode() == 101) btnNumber5.doClick();
		if(arg0.getKeyCode() == 102) btnNumber6.doClick();
		if(arg0.getKeyCode() == 103) btnNumber7.doClick();
		if(arg0.getKeyCode() == 104) btnNumber8.doClick();
		if(arg0.getKeyCode() == 105) btnNumber9.doClick();
	}
	

	
	private void tempAnswer(){
		switch(op){
			case 0: num1 = num1 + num2;
			break;
			
			case 1: num1 = num1 - num2;
			break;
			
			case 2: num1 = num1 * num2;
			break;
			
			case 3: num1 = num1 / num2;
			break;
		}
		
		str1 = "" + num1;		
		numDone = true;
	}
	
	class Clear implements ActionListener{
		public void actionPerformed(ActionEvent arg0) {
			num1 = 0;
			num2 = 0;
			str1 = "";
			str2 = "";
			
			txtNum.setText("");
			numDone = false;
			op = -1;
		}
	}

	class Point implements ActionListener{
		public void actionPerformed(ActionEvent arg0) {
			if(!numDone && boo1 == false){
				txtNum.append(".");
				str1 = str1 + ".";
				boo1 = true;
			} else if(numDone && boo2 == false){
				txtNum.append(".");
				str2 = str2 + ".";
				boo2 = true;
			}
			txtNum.requestFocusInWindow();
		}
	}
	
	class Enter implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			tempAnswer();
			txtNum.setText("" + num1);
			opOnScreen = false;
			txtNum.requestFocusInWindow();
		}
	}
	
	class Operator implements ActionListener {
		/* 0 = Plus
		 * 1 = Minus
		 * 2 = Multiply
		 * 3 = Divide
		 */
		public void actionPerformed(ActionEvent arg0) {			
			if(!opOnScreen) {
				str2 = "";
				num2 = 0;
				boo2 = false;
				boo1 = boo1 || boo2;
			}
			tempAnswer();
			
			
			if(arg0.getSource() == btnPlus){
				op = 0;
				txtNum.setText("" + num1 + "+");
			} else if(arg0.getSource() == btnMinus) {
				op = 1;
				txtNum.setText("" + num1 + "-");
			} else if(arg0.getSource() == btnMultiply) {
				op = 2;
				txtNum.setText("" + num1 + "*");
			} else if(arg0.getSource() == btnDivide) {
				op = 3;
				txtNum.setText("" + num1 + "/");
			}
			numDone = true;
			opOnScreen = true;
			txtNum.requestFocusInWindow();
		}
	}
	
	class Numbpad implements ActionListener {
		public void actionPerformed(ActionEvent arg0) {
			if(numDone && !opOnScreen) return;

			int num = 0;
			if(arg0.getSource() == btnNumber0) num = 0;
			else if(arg0.getSource() == btnNumber1) num = 1;
			else if(arg0.getSource() == btnNumber2) num = 2;
			else if(arg0.getSource() == btnNumber3) num = 3;
			else if(arg0.getSource() == btnNumber4) num = 4;
			else if(arg0.getSource() == btnNumber5) num = 5;
			else if(arg0.getSource() == btnNumber6) num = 6;
			else if(arg0.getSource() == btnNumber7) num = 7;
			else if(arg0.getSource() == btnNumber8) num = 8;
			else if(arg0.getSource() == btnNumber9) num = 9;
			
			if(!numDone) {
				str1 = str1 + num;
				num1 = Double.parseDouble(str1);
			}
			else {
				str2 = str2 + num;
				num2 = Double.parseDouble(str2);
			}
				
			txtNum.append("" + num);
			txtNum.requestFocusInWindow();
		}
	}
}

//Allright, yo
//scientific mode, 1
//sup//So what you wanna do
//go ahead
